package com.yun.util.yun.utils.IP;


/**
 * @Description 判断IP是否相互包含
 * @Copyright
 */
public  class IPIncludeHelper {
    /**
     * 判断ipA是否包含ipB
     * @param ipA
     * @param ipB
     * @return true:包含，false:不包含
     */
    public boolean CompareIPInclude(String ipA,String ipB)  throws Exception {
        if(ipA.equals(ipB))
        {
            return true;
        }
       /* if(ipA.contains(":"))
        {
            return CompareIPV6Include(ipA, ipB);
        }*/
        if(ipA.contains("."))
        {
            return CompareIPV4Include(ipA, ipB);
        }

        throw new Exception("IP中不包含“.”或者“:” 无法识别归属");
    }

    /**
     * IpV4包含判断
     * @param ipA
     * @param ipB
     * @return
     * @throws Exception
     */
    public boolean CompareIPV4Include(String ipA,String ipB) throws Exception {
        //A==B入口已返回
        //A唯一，B唯一，AB不同，则A不包含B
        if(!ipA.contains("/") && !ipB.contains("/") && !ipA.equals(ipB))
        {
            return false;
        }
        //A唯一，B不唯一，A不包含B 不含/的和含/32是相同的，所以后面计算的时候补全了
//        if(!ipA.contains("/") && ipB.contains("/"))
//        {
//            return false;
//        }
        //IP A起止
        IPNetWork ipv4NetWorkA= new IPNetWork().fromString(ipA);
        String[] startIpA=ipv4NetWorkA.getAddressFirst().split("\\.");
        System.out.println("AIPV4开始地址:"+ipv4NetWorkA.getAddressFirst());
        String[] endIpA=ipv4NetWorkA.getAddressLast().split("\\.");
        System.out.println("AIPV4结束地址:"+ipv4NetWorkA.getAddressLast());
        //A不唯一，B唯一(唯一的在转换的时候，调整为首尾相同)
//        if(!ipA.contains("/") && ipB.contains("/") )
//        {
//            String[] ipV6B= IPv6Address.fromString(ipB).toLongString().split(":");
//            return CompareIPV6Include( statrIpA,endIpA,ipV6B,ipV6B);
//        }
        //剩余 A不唯一，B不唯一
        //IP B起止
        IPNetWork ipv4NetWorkB=  new IPNetWork().fromString(ipB);
        String[] startIpB=ipv4NetWorkB.getAddressFirst().split("\\.");
        System.out.println("BIPV4开始地址:"+ipv4NetWorkB.getAddressFirst());
        String[] endIpB=ipv4NetWorkB.getAddressLast().split("\\.");
        System.out.println("BIPV4结束地址:"+ipv4NetWorkB.getAddressLast());
        //A不唯一，B不唯一
        return CompareIPInclude( startIpA,endIpA,startIpB,endIpB);
    }
    /**
     * IPV6判断包含
     * @param ipA
     * @param ipB
     * @return
     */
  /*  public boolean CompareIPV6Include(String ipA,String ipB) throws Exception {
        //A==B入口已返回
        //A唯一，B唯一，AB不同，则A不包含B
        if(!ipA.contains("/") && !ipB.contains("/") && !ipA.equals(ipB))
        {
            return false;
        }
        //A唯一，B不唯一，A不包含B
        if(!ipA.contains("/") && ipB.contains("/"))
        {
            return false;
        }
        //IP A起止
        IPv6Network ipv6NetWorkA= IPv6Network.fromString(ipA);
        String[] statrIpA=ipv6NetWorkA.getFirst().toLongString().split(":");
        System.out.println("Av6s是"+ipv6NetWorkA.getFirst().toLongString());
        String[] endIpA=ipv6NetWorkA.getLast().toLongString().split(":");
        System.out.println("Av6e是"+ipv6NetWorkA.getLast().toLongString());
        //A不唯一，B唯一
        if(!ipA.contains("/") && ipB.contains("/") )
        {
            String[] ipV6B= IPv6Address.fromString(ipB).toLongString().split(":");
            return CompareIPInclude( statrIpA,endIpA,ipV6B,ipV6B);
        }
        //剩余 A不唯一，B不唯一
        //IP B起止
        IPv6Network ipv6NetWorkB= IPv6Network.fromString(ipB);
        String[] statrIpB=ipv6NetWorkB.getFirst().toLongString().split(":");
        System.out.println("Bv6s是"+ipv6NetWorkB.getFirst().toLongString());
        String[] endIpB=ipv6NetWorkB.getLast().toLongString().split(":");
        System.out.println("Bv6e是"+ipv6NetWorkB.getLast().toLongString());
        //A不唯一，B不唯一
        return CompareIPInclude( statrIpA,endIpA,statrIpB,endIpB);
    }
*/
    /**
     * 判断IP是否包含
     * @param statrIpA
     * @param endIpA
     * @param statrIpB
     * @param endIpB
     * @return
     * @throws Exception
     */
    private boolean CompareIPInclude( String[] statrIpA,String[] endIpA, String[] statrIpB,String[] endIpB)throws Exception
    {
        //起始IP，A大于了B,则，A包含不了B
        if(CompareAGreaterB(statrIpA,statrIpB))
        {
            return false;
        }
        //结尾IP
        //结尾IP:B大于了A,则A包含不了B
        if(CompareAGreaterB(endIpB,endIpA))
        {
            return false;
        }
        return true;
    }
    /**
     * 判断A是否大于B
     * @param ipA
     * @param ipB
     * @return
     */
    private boolean CompareAGreaterB(String[] ipA,String[] ipB)
    {
        for(int i=0;i<ipA.length;i++)
        {
            if(Integer.parseInt(ipA[i],16) == Integer.parseInt(ipB[i],16))
            {
                continue;
            }
            if(Integer.parseInt(ipA[i],16) > Integer.parseInt(ipB[i],16)) {
                return true;
            }
            if(Integer.parseInt(ipA[i],16) < Integer.parseInt(ipB[i],16)) {
                return false;
            }
        }
        return false;
    }
}
